package com.huibao.hulibao.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.apkfuns.logutils.LogUtils;
import com.huibao.hulibao.R;
import com.huibao.hulibao.activity.StartHuliSelectedActivity;
import com.huibao.hulibao.entity.HuLiEntity;
import com.huibao.hulibao.utils.MyUtils;
import com.huibao.hulibao.utils.T;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by lizhaoyong on 2018/5/7.
 */
public class HuliSelectedtLvAdapter extends BaseAdapter {
    private ArrayList<String> mDatas;
    private Context mContext;
    private StartHuliSelectedActivity ssa;

    private LayoutInflater mInflater;

    public HuliSelectedtLvAdapter(ArrayList<String> datas, Context context, StartHuliSelectedActivity ssa) {
        this.mDatas = datas;
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
        this.ssa = ssa;
    }

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;

        // HuLiEntity.HuLiDetailData hd = mDatas.get(position);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.item_lv_start_huli_selected, parent, false);
            holder = new ViewHolder(convertView);
            // holder.mTextView = (TextView)convertView.findViewById(R.id.tv_title);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }


        holder.tv_lv_selected.setText(mDatas.get(position));
        holder.cb_selected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {

                    // T.show(mContext, "选中", 0);
                    ssa.saveSelectData(mDatas.get(position));
                } else {
                    // T.show(mContext, "未选中", 0);
                    ssa.deleteSelectedData(mDatas.get(position));
                }
            }
        });

        final CheckBox cb = holder.cb_selected;

        holder.rl_whole.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (cb.isChecked()) {
                    cb.setChecked(false);
                } else {
                    cb.setChecked(true);
                }
            }
        });


        return convertView;
    }


    static class ViewHolder {


        @BindView(R.id.tv_lv_selected)
        TextView tv_lv_selected;
        @BindView(R.id.cb_selected)
        CheckBox cb_selected;
        @BindView(R.id.rl_whole)
        RelativeLayout rl_whole;


        public ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
